package com.education.wisdom.controller;

import cn.hutool.core.date.DateUtil;
import com.education.common.utils.ApiReturnObject;
import com.education.common.utils.UUIDUtils;
import com.education.wisdom.feign.UserFeign;
import com.education.wisdom.pojo.Comments;
import com.education.wisdom.service.ICommentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wisdom/jrprofeuser")
public class JrCommentsController {

    @Autowired
    ICommentsService iCommentsService;

    @Autowired
    UserFeign userFeign;

    //添加数据
    @PostMapping("/addcomments")
    public ApiReturnObject insert(@RequestBody Comments comments){

        comments.setPlId(UUIDUtils.getByUUId());
        comments.setPlCreatetime(DateUtil.date());
        int i = iCommentsService.insertComments(comments);
        if(i > 0) return new ApiReturnObject("200", "success", "评论成功");
        return new ApiReturnObject("201", "warning", "评价失败");

    }

    //查询数据
    @GetMapping("/selectlist")
    public ApiReturnObject selectlist(Integer catalogId){
        List<Comments> comments = iCommentsService.selectByCatalogId(catalogId);
        for (Comments comment : comments) {
            Date plCreatetime = comment.getPlCreatetime();
            comment.setCreatetime(DateUtil.formatDateTime(plCreatetime));
            String plCreateuser = comment.getPlCreateuser();
            Map<String, Object> stringObjectMap = userFeign.selectByUserId(Integer.parseInt(plCreateuser));
            comment.setUserIcoUrl(stringObjectMap.get("image") + "");
            comment.setUserName(stringObjectMap.get("name") + "");
        }
        return new ApiReturnObject("200", "success", comments);

    }


}
